package com.fortune.service.impl;

import com.fortune.constants.Constants;
import com.fortune.dao.MProductInfoDao;
import com.fortune.dao.MProductStockDetailInfoDao;
import com.fortune.dao.ProductCategoryDao;
import com.fortune.dto.in.cOrderMgt.COrderDealerProduceDto;
import com.fortune.dto.in.dealaerProduce.*;
import com.fortune.dto.in.product.ProductAddDto;
import com.fortune.dto.in.product.ProductEditDto;
import com.fortune.dto.in.product.ProductListDto;
import com.fortune.dto.in.product.ProductUpAndDown;
import com.fortune.entity.ProductCategoryDo;
import com.fortune.entity.SimpleProduct;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.MProductInfoService;
import com.fortune.util.PinYinUtil;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.cOrderMgt.DealerProductList;
import com.fortune.vo.channelDiscountMgt.ProductInChannealDiscountMgt;
import com.fortune.vo.dProduct.DAllProduct;
import com.fortune.vo.dProduct.DProduceList;
import com.fortune.vo.dProduct.Discount;
import com.fortune.vo.productCategoryMgt.ProductCategoryVo;
import com.fortune.vo.productMgt.Product;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class MProductInfoServiceImpl implements MProductInfoService{
    @Autowired
    private MProductInfoDao mProductInfoDao;
    @Autowired
    private MProductStockDetailInfoDao mProductStockDetailInfoDao;

    @Autowired
    private ProductCategoryDao productCategoryDao;

    /**
     * 查询商品列表
     * @param companyId
     * @return
     */
    @Override
    public List<Product> queryProductInfoList(String companyId,ProductListDto productListDto) {
        return mProductInfoDao.queryProductInfoList(companyId,productListDto);
    }

    /**
     * 查询商品列表个数
     * @param companyId
     * @return
     */
    @Override
    public int queryProductInfoListCount(String companyId, ProductListDto productListDto) {
        return mProductInfoDao.queryProductInfoListCount(companyId,productListDto);
    }

    /**
     * 通过名称查询该商品是否存在
     * @param companyId
     * @param name
     * @return
     */
    @Override
    public int queryProductByName(String companyId, String name) {
        return mProductInfoDao.queryProductByName(companyId,name);
    }

    /**
     * 通过商品编号查询该商品是否存在
     * @param companyId
     * @param number
     * @return
     */
    @Override
    public int queryProductByNumber(String companyId, String number) {
        return mProductInfoDao.queryProductByNumber(companyId,number);
    }

    /**
     * 通过商品id查询该商品是否存在
     * @param companyId
     * @param produceId
     * @return
     */
    @Override
    public int queryProductByProduceId(String companyId, String produceId) {
        return mProductInfoDao.queryProductByProduceId(companyId,produceId);
    }

    /**
     * 新增商品
     * @param companyId
     * @param addDto
     * @return
     */
    @Override
    public int addProduce(String companyId, ProductAddDto addDto) {
        String pinyin = PinYinUtil.getInitials(addDto.getName());
        if (pinyin == null){
            pinyin = "";
        }
        return mProductInfoDao.addProduce(companyId,addDto,pinyin);
    }

    /**
     * 删除商品
     * @param companyId
     * @param produceId
     * @return
     */
    @Override
    public int deleteProduce(String companyId, String produceId) {
        return mProductInfoDao.deleteProduce(companyId,produceId);
    }

    /**
     * 编辑商品
     * @param companyId
     * @param editDto
     * @return
     */
    @Override
    public int editProductByProduceIdAndProduceId(String companyId, ProductEditDto editDto) {
        String pinyin = PinYinUtil.getInitials(editDto.getName());
        if (pinyin == null){
            pinyin = "";
        }
        return mProductInfoDao.editProductByProduceIdAndProduceId(companyId,editDto,pinyin);
    }

    /**
     * 商品上下架
     * @param companyId
     * @param upAndDown
     * @return
     */
    @Override
    public int upAndDownProduce(String companyId, ProductUpAndDown upAndDown) {
        return mProductInfoDao.upAndDownProduce(companyId,upAndDown);
    }

    /**
     * 查询商品统一批发价（对经销商）
     * @param companyId
     * @param produceId
     * @return
     */
    @Override
    public BigDecimal queryProducePrice(String companyId, String produceId) {
        return mProductInfoDao.queryProducePrice(companyId,produceId);
    }

    /**
     * 查询商品编号,名称,id
     * @param companyId
     * @return
     */
    @Override
    public List<ProductInChannealDiscountMgt> queryProduceDiscount(String companyId,int type) {
        return mProductInfoDao.queryProduceDiscount(companyId,type);
    }

    /**
     * 查询商品根据id
     * @param companyId
     * @param produceId
     * @return
     */
    @Override
    public Product queryProduceInfo(String companyId, String produceId) {
        return mProductInfoDao.queryProduceInfo(companyId,produceId);
    }

    /**
     * 编辑时根据名称和编号查询是否存在
     * @param companyId
     * @param editDto
     * @return
     */
    @Override
    public int queryEditProductByNameOrNumber(String companyId, ProductEditDto editDto) {
        return mProductInfoDao.queryEditProductByNameOrNumber(companyId,editDto);
    }

	@Override
	public List<SimpleProduct> listSimpleProductsByMId(String mId) {
		return mProductInfoDao.listSimpleProductsByMId(mId);
	}

    @Override
    public List<DealerProductList> queryMProduce(String dealerId,COrderDealerProduceDto cOrderDealerProduceDto) {
        return mProductInfoDao.queryMProduce(dealerId,cOrderDealerProduceDto);
    }

    @Override
    public List<DealerProductList> queryDProduce(String dealerId, COrderDealerProduceDto cOrderDealerProduceDto) {
        return mProductInfoDao.queryDProduce(dealerId,cOrderDealerProduceDto);
    }

    @Override
    public ProductStockDetailInfo queryIsStockExist(String companyId, String produceId) {
        return mProductStockDetailInfoDao.queryIsStockExist(companyId,produceId);
    }

    @Override
    public int queryProductCategory(String ppcid) {
        return productCategoryDao.queryProductCategoryFu(ppcid);
    }

    @Override
    public List<Product> queryProductInfoFuList(String companyId, ProductListDto productListDto) {
        List<ProductCategoryDo> productCategories = productCategoryDao.getAllProductCategories();
        List<ProductCategoryVo> vos = new ArrayList<>();
        LinkedList<ProductCategoryDo> linkedList = new LinkedList<>(productCategories);
        generateTree(vos, linkedList, productListDto.getPpcid());
        List<String> list = new ArrayList<>();
        getString(vos,list);
        List<Product> list1 = mProductInfoDao.queryProductInfoFuList(companyId,list,productListDto);
        return list1;
    }

    @Override
    public int queryProductInfoListFuCount(String companyId, ProductListDto productListDto) {
        List<ProductCategoryDo> productCategories = productCategoryDao.getAllProductCategories();
        List<ProductCategoryVo> vos = new ArrayList<>();
        LinkedList<ProductCategoryDo> linkedList = new LinkedList<>(productCategories);
        generateTree(vos, linkedList, productListDto.getPpcid());
        List<String> list = new ArrayList<>();
        getString(vos,list);
        int num = mProductInfoDao.queryProductInfoListFuCount(companyId,list,productListDto);
        return num;
    }

    @Override
    public int queryDProductByName(String did, String name) {
        return mProductInfoDao.queryDProductByName(did,name);
    }

    @Override
    public int queryDProductByNumber(String did, String number) {
        return mProductInfoDao.queryDProductByNumber(did,number);
    }

    @Override
    @Transactional
    public int addDProduce(String did, DProductAddDto addDto) {
        String pinyin = PinYinUtil.getInitials(addDto.getName());
        if (pinyin == null){
            pinyin = "";
        }
        String id = UUIDUtil.get32UUID();
        int success = mProductInfoDao.addDProduce(did,addDto,pinyin,id);
        if(success > 0){
            success = mProductInfoDao.addDProWholesale(did,addDto.getWholesalePrice(),id);
            if(success > 0){
                return success;
            }
        }else{
            throw new InsertInnerErrorException("新增失败");
        }
        return success;
    }

    @Override
    public int queryProductByDProduceId(String did, String produceId) {
        return mProductInfoDao.queryProductByDProduceId(did,produceId);
    }

    @Override
    public int queryEditDProductByName(String did, DProductEditDto editDto) {
        return mProductInfoDao.queryEditDProductByName(did,editDto);
    }

    @Override
    public int queryEditDProductByNumber(String did, DProductEditDto editDto) {
        return mProductInfoDao.queryEditDProductByNumber(did,editDto);
    }

    @Override
    @Transactional
    public int editProductByDProduceIdAndProduceId(String did, DProductEditDto editDto) {
        String pinyin = PinYinUtil.getInitials(editDto.getName());
        if (pinyin == null){
            pinyin = "";
        }
        int edit = mProductInfoDao.editProductByDProduceIdAndProduceId(did,editDto,pinyin);
        if(edit > 0){
            int delete = mProductInfoDao.deleteDProWholesalePrice(did,editDto.getId());
                if(delete > 0){
                    int success = mProductInfoDao.addDProWholesale(did,editDto.getWholesalePrice(),editDto.getId());
                    if(success > 0){
                        return success;
                    }else{
                        throw new UpdateInnerErrorException("新增失败");
                    }
                }else{
                    throw new UpdateInnerErrorException("删除失败");
                }
        }else{
            throw new UpdateInnerErrorException("编辑失败");
        }
    }

    @Override
    public int deleteDProduce(String did, String produceId) {
        return mProductInfoDao.deleteDProduce(did,produceId);
    }

    @Override
    public Product queryDProduceInfo(String did, String id) {
        return mProductInfoDao.queryDProduceInfo(did,id);
    }

    @Override
    public int upAndDownDProduce(String did, DProductUpAndDown upAndDown) {
        return mProductInfoDao.upAndDownDProduce(did,upAndDown);
    }

    @Override
    public List<Product> queryDProductInfoFuList(String did, DProductListDto productListDto) {
        List<ProductCategoryDo> productCategories = productCategoryDao.getAllProductCategories();
        List<ProductCategoryVo> vos = new ArrayList<>();
        LinkedList<ProductCategoryDo> linkedList = new LinkedList<>(productCategories);
        generateTree(vos, linkedList, productListDto.getPpcid());
        List<String> list = new ArrayList<>();
        getString(vos,list);
        List<Product> list1 = mProductInfoDao.queryDProductInfoFuList(did,list,productListDto);
        return list1;
    }

    @Override
    public int queryDProductInfoListFuCount(String did, DProductListDto productListDto) {
        List<ProductCategoryDo> productCategories = productCategoryDao.getAllProductCategories();
        List<ProductCategoryVo> vos = new ArrayList<>();
        LinkedList<ProductCategoryDo> linkedList = new LinkedList<>(productCategories);
        generateTree(vos, linkedList, productListDto.getPpcid());
        List<String> list = new ArrayList<>();
        getString(vos,list);
        int num = mProductInfoDao.queryDProductInfoListFuCount(did,list,productListDto);
        return num;
    }

    @Override
    public List<Product> queryDProductInfoList(String did, DProductListDto productListDto) {
        return mProductInfoDao.queryDProductInfoList(did,productListDto);
    }

    @Override
    public int queryDProductInfoListCount(String did, DProductListDto productListDto) {
        return mProductInfoDao.queryDProductInfoListCount(did,productListDto);
    }

    @Override
    public List<DProduceList> queryProduct(String did) {
        return mProductInfoDao.queryProduct(did);
    }

    @Override
    public List<DAllProduct> queryAllProduce(String mid,String did) {
        return mProductInfoDao.queryAllProduce(mid,did);
    }

    @Override
    public List<Discount> queryDiscount(DiscountDto dto) {
        return mProductInfoDao.queryDiscount(dto);
    }

    private void generateTree(List<ProductCategoryVo> productCategoryVos, LinkedList<ProductCategoryDo> categories, String pid) {
        for(int i=categories.size()-1;i>-1;i--) {
            if (categories.get(i).getPid().equals(pid)) {
                ProductCategoryVo pcVo = new ProductCategoryVo();
                pcVo.setId(categories.get(i).getId());
                pcVo.setPid(categories.get(i).getPid());
                pcVo.setName(categories.get(i).getName());
                productCategoryVos.add(pcVo);
                categories.remove(i);
            }
        }
        Collections.sort(productCategoryVos, new Comparator<ProductCategoryVo>() {
            @Override
            public int compare(ProductCategoryVo o1, ProductCategoryVo o2) {
                return -1;
            }
        });
        for (ProductCategoryVo vo : productCategoryVos) {
            vo.setProductCategories(new ArrayList<ProductCategoryVo>());
            generateTree(vo.getProductCategories(), categories, vo.getId());
        }
    }

    private void getString(List<ProductCategoryVo> productCategoryVos, List<String> list) {
        for (ProductCategoryVo vo : productCategoryVos) {
            list.add(vo.getId());
            if (vo.getProductCategories()!=null && vo.getProductCategories().size()>0){
                getString(vo.getProductCategories(),list);
            }
        }
    }

}
